﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BL.Entity;
using DL;
using System.Collections.ObjectModel;
namespace BL.Service
{
    public class tbl_UserService : ServiceBase
    {
        public List<User> GetAllUser()
        {
            var query = from item in this.Context.tbl_User
                        select new User()
                        {
                            Active = item.Active.Value,
                            CreateBy = item.CreateBy,
                            CreateDate = item.CreateDate.Value,
                            Email = item.Email,
                            Fax = item.Fax,
                            Name = item.Name,
                            Password = item.Password,
                            SerName = item.SerName,
                            Tel = item.Tel,
                            UpdateBy = item.UpdateBy,
                            UpdateDate = item.UpdateDate.Value,
                            UserID = item.UserID,
                            UserName = item.UserName
                        };
            return query.ToList();
        }
        public void AddUser(User userDTO)
        {

            if (GetUser(userDTO.UserName) != null) { throw new Exception("มีชื่อผู้ใช้นี้แล้วในระบบ"); }
            string encPassword = EncryptionService.Encryption(userDTO.Password);
            userDTO.Password = encPassword;

            DateTime dateGobla = base.GetDateGobal();

            tbl_User userItem = new tbl_User()
            {
                Active = userDTO.Active,
                UserName = userDTO.UserName,
                UpdateDate = dateGobla,
                UpdateBy = userDTO.UpdateBy,
                Tel = userDTO.Tel,
                SerName = userDTO.SerName,
                Password = userDTO.Password,
                Name = userDTO.Name,
                Fax = userDTO.Fax,
                Email = userDTO.Email,
                CreateDate = dateGobla,
                CreateBy = userDTO.CreateBy
            };
            this.Context.AddTotbl_User(userItem);
            this.Context.SaveChanges();
        }
        public void UpdateUser(User dto) {
            var query = from item in this.Context.tbl_User
                        where item.UserID == dto.UserID
                        select item;
            tbl_User update = query.FirstOrDefault();
            if (update != null)
            {
                update.Password = EncryptionService.Encryption(dto.Password);
                update.Name = dto.Name;
                update.Active = dto.Active;
                update.Email = dto.Email;
                update.Fax = dto.Fax;
                update.SerName = dto.SerName;
                update.Tel = dto.Tel;
                update.UpdateBy = update.UpdateBy;
                update.UpdateDate = base.GetDateGobal();
                this.Context.SaveChanges();
            }
            else {
                throw new Exception(string.Format("ไม่พบข้อมูลผู้ใช้ [{0}]",dto.UserName));
            }
        }
        public void DeleteUser(User dto) {
            var query = from item in this.Context.tbl_User
                        where item.UserID == dto.UserID
                        select item;
            tbl_User delete = query.FirstOrDefault();
            if (delete != null)
            {
                this.Context.DeleteObject(delete);
                this.Context.SaveChanges();
            }
            else
            {
                throw new Exception(string.Format("ไม่พบข้อมูลผู้ใช้ [{0}]", dto.UserName));
            }
        }
        public User GetUser(string userName)
        {
            User result;
            var query = from item in this.Context.tbl_User
                        where item.UserName == userName
                        select new User()
                        {
                            Active = item.Active.Value,
                            CreateBy = item.CreateBy,
                            CreateDate = item.CreateDate.Value,
                            Email = item.Email,
                            Fax = item.Fax,
                            Name = item.Name,
                            Password = item.Password,
                            SerName = item.SerName,
                            Tel = item.Tel,
                            UpdateBy = item.UpdateBy,
                            UpdateDate = item.UpdateDate.Value,
                            UserID = item.UserID,
                            UserName = item.UserName
                        };
            result = query.FirstOrDefault();
            return result; 
        }
        public User Login(User dto)
        {
            User login = new User();
            string password = EncryptionService.Encryption(dto.Password);
            var query = from item in this.Context.tbl_User
                        where item.UserName == dto.UserName && item.Password == password && item.Active==true
                        select item;
            tbl_User update = query.FirstOrDefault();
            if (update != null)
            {
                
                

                login.Name = update.Name;
                login.UserName = update.UserName;
                login.UserID = update.UserID;
            }
            else
            {
                throw new Exception(string.Format("ไม่พบข้อมูลผู้ใช้ [{0}]", dto.UserName));
            }
            return login;
        }
    }
}
